<template>
  <json-schema-form
      :formProps="{layoutColumn: 1}"
      :modelValue="categoryData"
      :schema="schema"
  >
    <template #default="{ formData, formRefFn }">
      <el-row>
        <el-col :lg="12" :sm="24">
          <el-input v-model="newVersionKey" placeholder="新版本key"></el-input>
        </el-col>
        <el-col :lg="12" :sm="24">
          <el-button v-show="newVersionKey" type="primary" @click="addNewVersion(formData.value)">
            新增版本
          </el-button>
        </el-col>
      </el-row>
      <el-row>
        <el-col :lg="24" :sm="24">
          <el-button type="primary" @click="saveData(formData.value)">
            保存数据
          </el-button>
        </el-col>
      </el-row>
      <pre style="background-color: #eee;">{{ JSON.stringify(formData.value, null, 4) }}</pre>
    </template>
  </json-schema-form>
</template>

<script lang="ts" setup>
import {ref} from "vue";
import JsonSchemaForm from '@lljj/vue3-form-element';
import {useUserOptionStore} from "@/core/common/StorageUtil";

const userOptionStore = useUserOptionStore();
const categoryData = userOptionStore.businessJsonData;

const newVersionKey = ref<string>('');
const schema = {
  "type": "object",
  "title": "业务JSON配置",
  "additionalProperties": false,
  "properties": {
    "category": {
      "type": "string",
      "title": "业务分类",
      "description": "业务分类KEY，需要保证不重复"
    },
    "processDesc": {
      "type": "string",
      "title": "流程描述简介"
    },
    "lastProcessKey": {
      "type": "string",
      "title": "最新版本流程ID",
      "description": "最新版本流程ID[支持SPEL]"
    },
    "lastProcessVersion": {
      "type": "string",
      "title": "最新版本流程版本号",
      "description": "最新版本流程版本号[支持SPEL]"
    },
    "searchConfig": {
      "type": "object",
      "title": "查询参数配置"
    },
    "admin": {
      "type": "object",
      "title": "管理平台配置"
    }
  },
  "required": [
    "category",
    "processDesc",
    "lastProcessKey"
  ],
  "default": {}
};

const addNewVersion = (val: any) => {
  if(!val.master){
    val.master = {};
  }
  val.master[newVersionKey.value] = {processKey: newVersionKey.value};
  newVersionKey.value = '';
}

const saveData = (val: any) => {
  userOptionStore.setBusinessJsonData(val);
}
</script>
<script lang="ts">
export default {
  name: "SillyCategoryPropertySchemaForm"
}
</script>